package com.wang.interview.tests.threadtest;

import java.util.concurrent.TimeUnit;

/**
 * @author: wang
 * @date: 2021/8/12 16:03
 * @description: 死锁模拟, 日志信息见TextT4
 */
public class T4 {
    private String lock1;
    private String lock2;

    public T4(String lock1, String lock2) {
        this.lock1 = lock1;
        this.lock2 = lock2;
    }

    public void method(){
        synchronized (this.lock1) {
            System.out.println(Thread.currentThread().getName()+"\t 单独持有");
            try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
            synchronized (this.lock2) {
                System.out.println(Thread.currentThread().getName()+"\t 共同持有");
            }
        }
    }

    public static void main(String[] args) {
        T4 t1 = new T4("lock1", "lock2");
        T4 t2 = new T4("lock2", "lock1");
        new Thread(()->{t1.method();},"a").start();
        new Thread(()->{t2.method();},"b").start();
    }
}
